<template>
  <div class="login">
    <div class="login_max">
      <div v-show="loginType" :class="[isUserLogin ? 'isnone' : '', 'userlog']">
        <div style="text-align: center; padding: 20px 0">
          <span style="font-weight: 600; font-size: 20px"
            >协和京山医院行政管理考核系统</span
          >
        </div>
        <abstract-form
          ref="form"
          :model="form"
          :rules="rules"
          action="#"
          method="post"
          class="user"
          label-width="0"
        >
          <abstract-form-item
            prop="tel"
            full
            size="medium"
            label-position="left"
          >
            <span class="svg-container">
              <v-icon icon="svg-user" />
            </span>
            <!-- <span class="iconfont iconshoujihao"></span> -->
            <el-input
              v-model="form.number"
              type="text"
              placeholder="人事编号"
              :class="[phoneiserr, 'inputSize']"
              @keydown.enter="login()"
            />
            <span :class="[phonenone, 'none']">{{ pleasephone }}</span>
          </abstract-form-item>
          <abstract-form-item prop="password" full>
            <div>
              <span class="svg-container">
                <v-icon icon="svg-password" />
              </span>
              <el-input
                v-model="form.password"
                type="password"
                placeholder="密码"
                :class="pwdiserr"
                @keydown.enter="login()"
              />
              <span :class="[pwdnone, 'none']">{{ pleasepwd }}</span>
            </div>
          </abstract-form-item>
        </abstract-form>
        <!-- <el-button @click="userLog()" type="primary" style="width:300px;"
          >登录</el-button> -->
        <el-button
          :loading="loading"
          type="primary"
          style="width: 300px"
          class="submit-btn"
          @click="login"
        >
          {{ loading ? '登 录 中...' : '登 录' }}
        </el-button>
        <!-- <el-button
          type="text"
          style="border: 0; background: white"
          @click="toggle"
          >{{ type == 'login' ? '注册' : '登录' }}
        </el-button> -->
      </div>
    </div>
  </div>
</template>
<script>
import config from '@/utils/config'
import { mapMutations } from 'vuex'
import { request } from '@/api/request'
import AbstractForm from '@/components/abstract/Form'
import AbstractFormItem from '@/components/abstract/Form/item'
import { elSuccess } from '@/utils/message'

export default {
  name: 'Login',

  components: {
    AbstractForm,
    AbstractFormItem
  },

  data() {
    return {
      loading: false,
      form: {
        number: '1',
        password: '123456'
      },
      rules: {
        number: [
          { required: true, message: '请输入人事编号', trigger: 'change' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'change' }]
      },
      codeLogin: '',
      isUserLogin: false,
      isRead: false,
      phoneNumber: '15486587486',
      number: '1',
      Pwd: '123456',
      phonenone: '',
      pwdnone: '',
      pleasephone: '请输入手机号',
      pleasepwd: '请输入密码',
      phoneiserr: '',
      pwdiserr: '',
      data: '',
      loginType: true,
      isRememberPwd: false,
      type: 'login'
    }
  },

  created() {},

  methods: {
    login() {
      this.$refs.form.validate((v) => {
        if (!v) return
        this.loading = true
        this.$store
          .dispatch('user/login', this.form)
          .then(this.success)
          .catch(() => (this.loading = false))
      })
    },

    success() {
      elSuccess('登录成功')
      const redirect = this.$route.query.redirect || '/'
      //由于清除消息时会造成卡顿，所以延迟0.2s跳转
      window.setTimeout(
        () => this.$router.replace(redirect).catch(() => {}),
        200
      )
    },

    async userLog() {
      if (this.phoneNumber == '') {
        this.pleasephone = '请输入手机号'
        this.phoneiserr = 'err'
        this.phonenone = 'block'
      } else {
        this.phonenone = ''
        this.phoneiserr = ''
      }
      if (this.Pwd == '') {
        this.pleasepwd = '请输入密码'
        this.pwdiserr = 'err'
        this.pwdnone = 'block'
      } else {
        this.pwdnone = ''
        this.pwdiserr = ''
        let params = {
          tel: this.phoneNumber,
          password: this.Pwd
        }
        if (this.type == 'login') {
          request('index/index/login', params, (res) => {
            if (res.code != 200) {
              return this.$message.error(res.data.split(':')[1])
            }
            this.$message.success('登录成功！')
            const userInfo = res.data
            config.setUserInfo(userInfo)
            // config.setToken()
            this.setUserInfo(userInfo)
            this.$router.push({
              path: '/'
            })
          })
        } else {
          params.repeat_pwd = params.password
          request('work/login/register', params, (res) => {
            if (res.code != 200) {
              return this.$message.error(res.data.split(':')[1])
            }
            this.toggle()
            this.$message.success('注册成功！')
          })
        }
      }
    },
    toggle() {
      if (this.type == 'login') {
        this.type = 'register'
      } else {
        this.type = 'login'
      }
    },
    ...mapMutations({ setUserInfo: 'app/SET_USER_INFO' })
  }
}
</script>
<style>
html,
body {
  background: #f4f6ff;
  width: 100%;
  height: 100%;
}
</style>

<style lang="scss" scoped>
body {
  background: #f4f6ff;
}
input {
  border: 1px solid gainsboro;
  outline: none;
}
.isnone {
  display: none;
}
.none {
  display: none;
  color: red;
}

.block {
  display: block;
}
.login_max {
  width: 400px;
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.15);
  //   height: 350px;
  text-align: center;
  margin: auto;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
  padding-bottom: 20px;
  .shou {
    text-align: center;
    height: 50px;
    line-height: 50px;
    font-size: 25px;
    width: 145px;
  }
  .user {
    width: 300px;
    margin: 20px auto;
    // div {
    //   width: 100%;
    //   height: 40px;
    //   margin-top: 30px;
    //   position: relative;
    //   input {
    //     width: 100%;
    //     height: 100%;
    //     padding: 0 15px 0 58px;
    //   }
    //   span:first-child {
    //     width: 16px;
    //     height: 16px;
    //     position: absolute;
    //     left: 21px;
    //     top: 11px;
    //     // background: url(https://xinhehonggu.com/_nuxt/img/c180b47.png) no-repeat -371px -419px;
    //   }
    // }
  }

  .login_sel {
    width: 390px;
    height: 21px;
    margin: 20px auto 0;
    font-size: 12px;
    color: #249dff;
    user-select: none;
    div:nth-child(1) {
      float: left;
      cursor: pointer;
    }

    a,
    span {
      margin-right: 5px;
    }
    .unautologin {
      width: 14px;
      height: 14px;
      color: #fff;
      background: #fff;
      text-align: center;
      border: 1px solid #249dff;
      line-height: 11px;
    }
    .autologinchecked {
      width: 14px;
      height: 14px;
      color: #fff;
      background: #249dff;
      text-align: center;
      border: 1px solid #249dff;
      line-height: 11px;
    }
    div {
      float: right;
    }
  }
  button {
    display: block;
    height: 40px;
    margin: auto;
    margin-bottom: 20px;
    background: #fff;
    color: #249dff;
    border: 1px solid #249dff;
    border-radius: 5px;
    outline: none;
    &:nth-of-type(1) {
      margin-top: 30px;
      background: #249dff;
      color: #fff;
    }
    &:nth-of-type(1):hover {
      opacity: 0.8;
    }
    &:nth-of-type(2):hover {
      background: #ecf5ff;
    }
  }
  .code {
    width: 549px;
    margin: auto;
    position: relative;
    div:nth-child(1) {
      span {
        position: absolute;
      }
      span:nth-of-type(1) {
        width: 16px;
        height: 26px;
        left: 21px;
        top: 7px;
        background: url(https://xinhehonggu.com/_nuxt/img/c180b47.png) no-repeat -371px -419px;
      }
      span:nth-of-type(2) {
        right: 8px;
        top: 10px;
        color: #249dff;
      }
    }
    div {
      width: 390px;
      height: 40px;
      margin: 30px auto 0;
      position: relative;
      input {
        outline: none;
        width: 100%;
        height: 100%;
        padding: 0 15px 0 58px;
      }
    }
    .yzm {
      margin-bottom: 20px;
      span {
        width: 48px;
        height: 30px;
        border-bottom: 2px solid gainsboro;
        margin-right: 20px;
        &:last-child {
          margin-right: 0;
        }
      }
    }
    .yzminput {
      width: 100%;
      height: 40px;
      position: absolute;
      top: 60px;
      left: 80px;
      border: none;
      z-index: 999;
      outline: none;
      background: rgba(0, 0, 0, 0);
      padding-left: 20px;
      letter-spacing: 60px;
      caret-color: rgba(0, 0, 0, 0);
    }
  }

  .codeLogin_sel {
    width: 390px;
    margin: auto;
    font-size: 12px;
    color: #249dff;
    cursor: pointer;
    div:nth-child(1) {
      height: 20px;
      float: left;
      width: 100%;
    }
    div:nth-child(2) {
      float: right;
      margin-bottom: 20px;
    }
    .read {
      width: 14px;
      height: 14px;
      color: #fff;
      background: #fff;
      text-align: center;
      border: 1px solid #249dff;
      line-height: 11px;
    }
    .unread {
      width: 14px;
      height: 14px;
      color: #fff;
      background: #249dff;
      text-align: center;
      border: 1px solid #249dff;
      line-height: 11px;
    }
    span {
      margin-left: 5px;
      margin-bottom: 20px;
    }
    a {
      margin-left: 5px;
    }
  }
}
.qcwarp {
  width: 180px;
  height: 180px;
  background-color: #180;
  position: absolute;
  right: 50%;
  transform: translate(50%, 40%);
}
.tiptip {
  position: fixed;
  bottom: 45px;
  margin-left: -30px;
}
.remember {
  width: 200px;
  float: left;
  margin-bottom: 25px;
  margin-top: -40px;
  margin-left: -11px;
}
</style>

<style lang="scss" src="./style.scss"></style>
